package com.dhecp.project.system.controller;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.project.system.service.SysUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @program: kjecp
 * @description: 用户信息
 * @author: hlf
 * @create: 2023-06-01 09:30
 */
@Controller
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    private String prefix = "system/user";
    private String moduleTable = ModuleTable.USER_INFO;
    private String moduleTableForRole = ModuleTable.ROLE_INFO;

    @Autowired
    private SysUserInfoService userInfoService;

    /**
     * 修改密码——数据提交
     * @param upType 修改类型（"resetpwd":重置密码，无需提供原密码；"changepwd":修改密码，需要提供原密码）
     * @param userId 用户编号
     * @param newPwd 新用户密码
     * @param oldPwd 原用户密码，修改密码时需要提供，重置密码时可不提供
     * @return AjaxResult
     * @author DH-Xxj
     */
//    @RequiresPermissions("system:user:edit")
    @Log(title = "修改密码", businessType = BusinessType.UPDATE)
    @PostMapping("/editpwdpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult userPassWordEditPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= userInfoService.updateUserPassWord(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 修改二级密码——数据提交
     * @param upType 修改类型（"resetpwd":重置密码，无需提供原密码；"changepwd":修改密码，需要提供原密码）
     * @param userId 用户编号
     * @param newPwd2 新二级密码
     * @param oldPwd2 原二级密码，修改密码时需要提供，重置密码时可不提供
     * @return AjaxResult
     * @author DH-Xxj
     */
//    @RequiresPermissions("system:user:edit")
    @Log(title = "修改二级密码", businessType = BusinessType.UPDATE)
    @PostMapping("/editpwd2post")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult userPassWord2EditPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= userInfoService.updateUserPassWord2(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 修改用户信息——数据提交
     * @param userInfoItems 必要参数：对应实体类的json格式字符串
     * @param upType 必要参数：修改类型（修改用户信息：upuser，修改用户基础信息：upuserbaseinfo，修改用户皮肤：upskin，修改档案照片：upuserphoto，修改用户头像：upuserpicture）
     * @return AjaxResult
     * @author DH-Xxj
     */
    // @RequiresPermissions("system:user:edit")
    @Log(title = "修改用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult userInfoEditPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (params.get("userInfoItems") == null || params.get("userInfoItems") == ""){
            return AjaxResult.error("缺少参数userInfoItems【对应实体类的json格式字符串】。");
        }
        try {
            ajaxResult= userInfoService.updateUserInfo(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}